//
//  TMoperateController.swift
//  GZWeibo007
//
//  Created by myd on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
class TMoperateController: UIViewController {
    //这是输入提示
    var text :String = ""{
        
        didSet{
            labin.text = text
            if text == "转发" || text == "发送"
            {
                labin.text = "请输入\(text)内容..."
             
            }
               self.setNotification()
            
        }
    }
    //设置通知
    func setNotification()
    {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "lookinttext", name: UITextViewTextDidChangeNotification, object: lab)
    }
    func lookinttext()
    {
        if lab.text.characters.count != 0
        {
            labin.hidden = true
            if text != "评论"{
            if lab.text.characters.count > 140
            {
                SVProgressHUD.showErrorWithStatus("输入内容不能超过140个字符")
                let tex = lab.text as NSString
                lab.text = tex.substringToIndex(140)
            }}
            
            return
        }
        labin.hidden = false
        
    }
    var titlestr : String?
    var myname : String?
        {
        didSet{
       titleusername = myname
        }
    }
    var sta : CZStatus?
        {
        didSet{
            
            titleusername=CZUserAccount.loadUserAccount()?.screen_name
        }
    }
    let titleviews = titleview()
    var titleusername : String?
    
    ///转发,评论文本
    var lab : UITextView = {
        let lab1 = UITextView()
        lab1.textAlignment = NSTextAlignment.Left
        lab1.font = UIFont.systemFontOfSize(14)
        lab1.backgroundColor = UIColor.clearColor()
        return lab1
    }()
    var labin : UILabel = {
        let lab1 = UILabel()
        lab1.font = UIFont.systemFontOfSize(14)
        lab1.textColor = UIColor.lightGrayColor()
        return lab1
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = UIRectEdge.None
        setview()
        self.lab.becomeFirstResponder()
        // Do any additional setup after loading the view
    }
    
    //设置空间布局和title
    func setview()
    {
        titleviews.frame = CGRectMake(0, 0, 100, 50)
        
        ///文本的布局.
        lab.frame = CGRectMake(8,0, self.view.bounds.width-16, self.view.bounds.height)
        labin.frame = CGRectMake(10, 8, 0, 0)
        labin.sizeToFit()
        titleviews.title.text = titlestr! + "微博"
        titleviews.username.text = titleusername
        self.navigationItem.titleView = titleviews
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: titlestr, style: UIBarButtonItemStyle.Plain, target: self, action: "work")
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "back")
        self.view.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(labin)
        self.view.addSubview(lab)
        
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //返回
    func back()
    {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    //点击按钮响应对应事件
    func work()
    {
        if titlestr == "评论"
        {
            if lab.text?.characters.count == 0
            {
                SVProgressHUD.showErrorWithStatus("输入不能为空")
                return
            }
            CZNetworkTool.sharedInstance.commentscreate(sta!.id, comment: lab.text!, finsh: { (result, error) -> () in
                if result == nil || error != nil
                {
                    SVProgressHUD.showErrorWithStatus("评论失败")
                    return
                }
                SVProgressHUD.showSuccessWithStatus("评论成功")
                self.dismissViewControllerAnimated(true, completion: nil)
            })
        }else if titlestr == "转发"
        {
            CZNetworkTool.sharedInstance.statusesrepost(sta!.id, text: lab.text!, finsh: { (result, error) -> () in
                if result == nil || error != nil
                {
                    SVProgressHUD.showErrorWithStatus("转发失败")
                    return
                }
                
                SVProgressHUD.showSuccessWithStatus("转发成功")
                self.dismissViewControllerAnimated(true, completion: nil)
                
            })
        }else if titlestr == "发送"
        {
       CZNetworkTool.sharedInstance.sent(lab.text, finsh: { (result, error) -> () in
        
        if result == nil || error != nil
        {
       SVProgressHUD.showErrorWithStatus("发送失败")
            return
        }
       self.dismissViewControllerAnimated(true, completion: nil)
        SVProgressHUD.showSuccessWithStatus("发送成功")
       })
        }
    }
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}

//创建titleview的view
class titleview : UIView {
    let title = UILabel()
    let username = UILabel()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setview()
    }
    func setview()
    {
        addSubview(title)
        addSubview(username)
        
        title.textColor = UIColor.blackColor()
        title.font = UIFont.systemFontOfSize(18)
        title.textAlignment = NSTextAlignment.Center
        
        username.font = UIFont.systemFontOfSize(11)
        username.textColor = UIColor.lightGrayColor()
        username.textAlignment = NSTextAlignment.Center
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        title.frame = CGRectMake(0, 0, self.bounds.size.width,self.bounds.size.height*0.5)
        username.frame = CGRectMake(0, self.bounds.size.height*0.5,self.bounds.size.width,self.bounds.size.height*0.5)
    }
}
